#ifndef TRANSLATORAGENTWINDOW_H
#define TRANSLATORAGENTWINDOW_H

#include "QHotkey"
#include <QMainWindow>
#include <QPoint>
#include <QProcess>
#include <QSizeGrip>
#include <QResizeEvent>

class QFrame;
class QVBoxLayout;
class QWidget;
class QPushButton;
class QMouseEvent;
class QStackedWidget;
class QWebEngineView;
class LoadingAnimation;
class QPixmap;
class QNetworkAccessManager; // 用于管理网络请求
class QNetworkReply;         // 代表一个网络响应
class QTimer;
class QTcpSocket;


class TranslatorAgentWindow : public QMainWindow
{
    Q_OBJECT
protected:
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void showEvent(QShowEvent* event) override;
    void resizeEvent(QResizeEvent *event) override;
    bool eventFilter(QObject *watched, QEvent *event) override;

public:
    TranslatorAgentWindow(QWidget *parent = nullptr);
    ~TranslatorAgentWindow();

private:

    void startNewConversation();
    void showLoading(bool show);
    void updateContent(const QString &html);                    //只负责解决展示内容的闪烁问题
    void updatePinStatus();

private slots:
    void onScreenshotInitiated();                     // 当截图被触发时调用
    void onScreenshotFinished(const QPixmap &pixmap); // 当截图完成时调用
    void onPinButtonClicked();                        //控制置顶按钮点击后调用
    void onOcrRequestFinished(QNetworkReply *reply);  //处理OCR网络请求完成的槽函数

    // 处理TCP连接
    void onAiSocketConnected();
    void onAiSocketDisconnected();
    void onAiSocketReadyRead();

private:
    QFrame *m_centralWidget;                //主窗口
    QVBoxLayout *m_mainLayout;              //垂直布局
    QWidget *m_toolbar;                     //工具栏
    QPushButton *m_newChatButton;           //新对话按钮
    QPushButton *m_screenshotButton;        //截图按钮
    QPushButton *m_pinButton;               //控制置顶按钮
    bool m_isPinned;                        //当前是否置顶

    QPoint m_dragPos;                       //前一个点

    QStackedWidget *m_stackedWidget;        //卡片组，存储加载动画和加载翻译的内容
    QWebEngineView *m_contentView;          //翻译显示的内容
    LoadingAnimation *m_loadingWidget;      //动画Widget

    QHotkey* m_hotkey;                      //管理全局快捷键
    QNetworkAccessManager *m_networkManager;//网络管理器
    QProcess *m_p2tProcess;                 //p2t文字提取

    QTcpSocket *m_aiSocket;                 //socket
    QString m_currentMarkdown;              //拼接数据

    QSizeGrip *m_sizeGrip;                  //调整大小的手柄

};
#endif // TRANSLATORAGENTWINDOW_H
